package com.scsj.supermarket.view.fragment.fivepagemodel;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.orhanobut.logger.Logger;
import com.scsj.supermarket.R;
import com.scsj.supermarket.adapter.MineAdapter;
import com.scsj.supermarket.base.QueryOrderNumBase;
import com.scsj.supermarket.bean.MineBean;
import com.scsj.supermarket.bean.OrderNumBean;
import com.scsj.supermarket.bean.SelectMyAdvertisingSpaceBean;
import com.scsj.supermarket.config.RxSPConfig;
import com.scsj.supermarket.presenter.OrderNumPresenter;
import com.scsj.supermarket.utils.SkipUtils;
import com.scsj.supermarket.view.fragment.basemodel.BaseFragment;
import com.vondear.rxtools.RxSPTool;
import com.vondear.rxtools.RxTool;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的页面
 */

public class MineFragment extends BaseFragment implements View.OnClickListener, QueryOrderNumBase.IView {

    private OrderNumPresenter orderNumPresenter;
    public final static int mine_all_order = 0; //全部订单
    public final static int mine_pay_order = 1; //代付款
    public final static int mine_send_order = 2;//待发货
    public final static int mine_receive_order = 3; //待收货
    public final static int mine_complete_order = 4; //已完成
    public final static int mine_payment_order = 4; //退款/售后
    private ScrollView scroll;
    private RelativeLayout mine_background;//我的-顶部背景
    private LinearLayout login_ll;//  登录栏
    private ImageView setting_iv;
    private RelativeLayout msg_iv;//右上角信息框
    private LinearLayout user_common_ll, user_store_ll;//普通用户菜单 商家菜单
    //登录/未登录时-我的钱包
    //                         会员卡         积分        积分宝
    private LinearLayout integral_ll, integral_treasure_ll;
    //                        优惠券         会员码          钱包
    private LinearLayout coupon_ll, member_card_ll, wallet_ll;
    private LinearLayout wait_to_pay_ll, wait_for_delivery_ll, wait_for_receive_goods_ll, order_completed_ll, refund_ll;
    //                     商品关注        店铺关注        商品关注             浏览记录
    private LinearLayout follow_goods_ll, follow_shop_ll, follow_goods_ll1, browsing_history_ll;
    private RecyclerView mine_func_rv;
    private List<MineBean> dataMyFuncBeans;
    private MineAdapter myFuncAdapter;
    private RecyclerView store_menu_rv;
    private List<MineBean> storeMenuBeans;
    private MineAdapter storeMenuAdapter;
    private ImageView adIv;
    private TextView mineOrder;
    private TextView mNickname_tv;
    private ImageView mHeadIv;
    private TextView mPhone_num_tv;
    public static final String TAG = "我的";
    public static int isLogin = 1;//临时是否登录标记等
    private SwipeRefreshLayout swipeRefreshLayout;
    private View view;
    private LinearLayout mCollectLl;
    private LinearLayout mCommentLl;
    private LinearLayout mShippingAddressLl;
    private LinearLayout mGeneralizeLl;
    private LinearLayout mCustomerServiceCenterLl;
    private LinearLayout mCooperationLl;
    private TextView mAllOrdersTv;
    private RelativeLayout mMinewOrder;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_five, null);

        swipeRefreshLayout = view.findViewById(R.id.mine_srl);
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);

        initView(view, null);
        initData();
        initListener();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        setUserStatus();
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        orderNumPresenter = new OrderNumPresenter(this);
        adIv = view.findViewById(R.id.ad_img);
        mine_func_rv = view.findViewById(R.id.mine_func_rv);
        mine_func_rv.setLayoutManager(new GridLayoutManager(getHoldingActivity(), 4) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        store_menu_rv = view.findViewById(R.id.store_menu_rv);
        mine_background = view.findViewById(R.id.mine_background);//我的-顶部背景


        setting_iv = view.findViewById(R.id.setting_iv);
        msg_iv = view.findViewById(R.id.msg_iv);
        login_ll = view.findViewById(R.id.login_ll);//登录栏
        mNickname_tv = view.findViewById(R.id.nickname_tv);//昵称
        mHeadIv = view.findViewById(R.id.head_iv);
        mPhone_num_tv = view.findViewById(R.id.phone_num_tv);
        coupon_ll = view.findViewById(R.id.discount_coupon_ll);//优惠券
        member_card_ll = view.findViewById(R.id.member_code_ll);//会员码
        wallet_ll = view.findViewById(R.id.my_wallet_ll);//我的钱包
        integral_ll = view.findViewById(R.id.integral_ll);//积分
        integral_treasure_ll = view.findViewById(R.id.integral_treasure_ll);//积分宝
        user_common_ll = view.findViewById(R.id.user_common_ll);//普通用户菜单
        user_store_ll = view.findViewById(R.id.user_store_ll);//商家菜单
        //全部订单
        mMinewOrder = view.findViewById(R.id.mine_order_rl);
        mAllOrdersTv = view.findViewById(R.id.all_orders_tv);
        //订单列表控件
        wait_to_pay_ll = view.findViewById(R.id.wait_to_pay_ll);
        wait_for_delivery_ll = view.findViewById(R.id.wait_for_delivery_ll);
        wait_for_receive_goods_ll = view.findViewById(R.id.wait_for_receive_goods_ll);
        order_completed_ll = view.findViewById(R.id.order_completed_ll);
        refund_ll = view.findViewById(R.id.refund_ll);
        //订单列表控件
        follow_goods_ll = view.findViewById(R.id.follow_goods_ll);//商品关注
        follow_shop_ll = view.findViewById(R.id.follow_shop_ll);//店铺关注
        follow_goods_ll1 = view.findViewById(R.id.follow_goods_ll1);//店铺关注
        browsing_history_ll = view.findViewById(R.id.browsing_history_ll);//浏览记录
        mCollectLl = (LinearLayout) view.findViewById(R.id.collect_ll);//收藏
        mCollectLl.setOnClickListener(this);
        mCommentLl = (LinearLayout) view.findViewById(R.id.comment_ll);//评论
        mCommentLl.setOnClickListener(this);
        mShippingAddressLl = (LinearLayout) view.findViewById(R.id.shipping_address_ll);//收货地址
        mShippingAddressLl.setOnClickListener(this);
        mGeneralizeLl = (LinearLayout) view.findViewById(R.id.generalize_ll);//我要推广
        mGeneralizeLl.setOnClickListener(this);
        mCustomerServiceCenterLl = (LinearLayout) view.findViewById(R.id.customer_service_center_ll);//客服中心
        mCustomerServiceCenterLl.setOnClickListener(this);
        mCooperationLl = (LinearLayout) view.findViewById(R.id.cooperation_ll);//我要合作
        mCooperationLl.setOnClickListener(this);
    }

    @Override
    protected void initListener() {
        setting_iv.setOnClickListener(this);
        msg_iv.setOnClickListener(this);
        login_ll.setOnClickListener(this);
        wallet_ll.setOnClickListener(this);
        coupon_ll.setOnClickListener(this);
        member_card_ll.setOnClickListener(this);

        integral_ll.setOnClickListener(this);
        integral_treasure_ll.setOnClickListener(this);


        //订单列表控件
        mMinewOrder.setOnClickListener(this);
        wait_to_pay_ll.setOnClickListener(this);
        wait_for_delivery_ll.setOnClickListener(this);
        wait_for_receive_goods_ll.setOnClickListener(this);
        order_completed_ll.setOnClickListener(this);
        refund_ll.setOnClickListener(this);
        //订单列表控件

        follow_goods_ll.setOnClickListener(this);
        follow_goods_ll1.setOnClickListener(this);
        follow_shop_ll.setOnClickListener(this);
        browsing_history_ll.setOnClickListener(this);

        adIv.setOnClickListener(this);

    }

    @Override
    protected void initData() {

        //页面填充
        if (RxSPTool.getBoolean(getHoldingActivity(), RxSPConfig.IS_LOGIN)) {
            mNickname_tv.setText(RxSPTool.getString(getHoldingActivity(), RxSPConfig.USER_NICKNAME));
            Glide.with(getHoldingActivity())
                    .load(RxSPTool.getString(getHoldingActivity(), RxSPConfig.USER_AVATAR))
                    .error(R.mipmap.ic_head_portrait)
                    .into(mHeadIv);
            mPhone_num_tv.setText(RxSPTool.getString(getHoldingActivity(), RxSPConfig.USER_PHONE));
        }

        dataMyFuncBeans = new ArrayList<>();
        myFuncAdapter = new MineAdapter(dataMyFuncBeans);
        mine_func_rv.setAdapter(myFuncAdapter);

        store_menu_rv.setLayoutManager(new GridLayoutManager(getHoldingActivity(), 5) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        storeMenuBeans = new ArrayList<>();
        storeMenuAdapter = new MineAdapter(storeMenuBeans);
        store_menu_rv.setAdapter(storeMenuAdapter);


        orderNumPresenter.deliverData();

//        String loginDataBean = RxSPTool.getString(getHoldingActivity(), RxSPConfig.LOGIN_DATABEAN);
//        Logger.d("登录信息==>"+loginDataBean);

    }

    @Override
    protected int getLayoutId() {
        return 0;
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.customer_service_center_ll://客服中心
                SkipUtils.toCustomerServicePage(getHoldingActivity());
                break;
            case R.id.setting_iv://设置中心
                SkipUtils.toNewSetting(getHoldingActivity());
                break;
            case R.id.msg_iv://右上角消息
                SkipUtils.toMessage(getHoldingActivity());
                break;
            case R.id.login_ll://头像及昵称 账户设置
                if (RxSPTool.getBoolean(getHoldingActivity(), RxSPConfig.IS_LOGIN)) {
                    SkipUtils.toPersonalInformation(getHoldingActivity());
                } else {
                    SkipUtils.toLogin(getHoldingActivity());
                }
                break;
            case R.id.my_wallet_ll://我的钱包
                SkipUtils.toMyWallet(getHoldingActivity());
                break;
            case R.id.discount_coupon_ll://优惠券
                SkipUtils.toCoupou(getHoldingActivity());
                break;
            case R.id.member_code_ll://会员码
                // SkipUtils.toBusinessCircleDetail(getHoldingActivity(), tradeId);
                break;
            case R.id.mine_order_rl://查看全部订单
                SkipUtils.toMyOrder(getHoldingActivity(), MineFragment.mine_all_order);
                break;
            case R.id.wait_to_pay_ll://代付款
                SkipUtils.toMyOrder(getHoldingActivity(), MineFragment.mine_pay_order);
                break;
            case R.id.wait_for_delivery_ll://代发货
                SkipUtils.toMyOrder(getHoldingActivity(), MineFragment.mine_send_order);
                break;
            case R.id.wait_for_receive_goods_ll://待收货
                SkipUtils.toMyOrder(getHoldingActivity(), MineFragment.mine_receive_order);
                break;
            case R.id.order_completed_ll://已完成
                SkipUtils.toMyOrder(getHoldingActivity(), MineFragment.mine_complete_order);
                break;
            case R.id.refund_ll://待付款
                SkipUtils.toMyOrder(getHoldingActivity(), MineFragment.mine_payment_order);
                break;
            case R.id.collect_ll://收藏
                SkipUtils.toMineCollection(getHoldingActivity());
                break;
            case R.id.comment_ll://评论
//                SkipUtils.toMineCommentPage(getHoldingActivity());
                SkipUtils.toAllGoodsCommentPage(getHoldingActivity());
                break;
            case R.id.shipping_address_ll://收货地址
                SkipUtils.toAdress(getHoldingActivity());
                break;
            case R.id.generalize_ll://我要推广
                SkipUtils.toGeneralize(getHoldingActivity());
                break;
            case R.id.cooperation_ll://我要合作
                SkipUtils.toToCooperation(getHoldingActivity());
                break;
            case R.id.ad_img:
                //TODO
                //暂时去商品详情页面
                SkipUtils.toGoodsDetails(getHoldingActivity(), 1);

            default:
                break;
        }
    }

    private void setUserStatus() {
        if (isLogin == 0) {//未登录

            login_ll.setVisibility(View.GONE);
            user_common_ll.setVisibility(View.VISIBLE);
            user_store_ll.setVisibility(View.GONE);
        } else if (isLogin == 1) {//已登录
            mine_background.setBackgroundResource(R.mipmap.mine_background_login);

            login_ll.setVisibility(View.VISIBLE);
            user_common_ll.setVisibility(View.VISIBLE);
            user_store_ll.setVisibility(View.GONE);
        } else if (isLogin == 2) {//商家中心
            mine_background.setBackgroundResource(R.mipmap.mine_background_login);

            login_ll.setVisibility(View.VISIBLE);
            user_common_ll.setVisibility(View.GONE);
            user_store_ll.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showSuccessMsg(String s, OrderNumBean orderNumBean) {

    }

    /**
     * App查询“我的”页面中广告位
     */
    @Override
    public void showADSuccessMsg(String s, SelectMyAdvertisingSpaceBean bean) {

    }

    @Override
    public void showErrorMsg(String s) {

    }
}
